package de.edb.ctrl;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import de.edb.dao.ArtistDao;
import de.edb.dao.DaoException;
import de.edb.dao.EventDao;
import de.edb.dao.LocationDao;
import de.edb.dao.UserDao;
import de.edb.model.Event;
import de.edb.model.User;
import java.text.SimpleDateFormat;



@Controller
public class EventController {
    private EventDao eventDao = null;
    private ArtistDao artistDao = null;
    private LocationDao locationDao = null;
    private UserDao userDao = null;
    
    public void setEventDao(EventDao eventDao) {
		this.eventDao = eventDao;
	}

	public void setArtistDao(ArtistDao artistDao) {
		this.artistDao = artistDao;
	}

	public void setLocationDao(LocationDao locationDao) {
		this.locationDao = locationDao;
	}
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	
    @RequestMapping("/index.html")
    public ModelAndView home() throws DaoException {
    	
    	
		ModelAndView mv = new ModelAndView();
		mv.addObject("nextevents", eventDao.findFutureDTO());
		mv.setViewName("index");
		mv.addObject("tab", "home");
		
		//create a default user if no users are present
		if(userDao.findAll().size()==0) userDao.create("admin", "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918");
		
		//generate a warning if default user with default password is active
		java.util.List<User> usrlist = userDao.findByName("admin");
		if(usrlist.size()>=1){
			User a = usrlist.get(0);
			if(a.getName().equals("admin") && a.getPassword().equals("8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918")){
				mv.addObject("warning", "<h2>Sicherheitswarnung</h2><p>Der voreingestellte Benutzeraccount 'admin' mit dem Passwort 'admin' wurde automatisch angelegt und ist aktiv. Diese Warnung verschwindet erst, wenn dieser entweder gelöscht oder sein Passwort geändert wird</p>");
			}
		}
				
		return mv;
    }
	        
	
	/**
     * <p>Searches for all {@link Event} objects and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/events.html'.</p>
     */
	
    @RequestMapping(value="/events.html", method=RequestMethod.GET)
    public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Events");
		mv.addObject("message", "Alle Events");
		mv.addObject("events", eventDao.findFutureDTO());
		mv.addObject("pevents", eventDao.findPastDTO());
		mv.addObject("tab", "events");
		mv.setViewName("list-events");
		return mv;
    }

    
	/**
     * <p>Creates a new {@link Event} object</p>
     * 
     * <p>Expected HTTP GET and request '/newevent.html'.</p>
     */
    @RequestMapping(value="/admin/newevent.html", method=RequestMethod.GET)
    public ModelAndView newEvent() {
    	ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Event eintragen");
		mv.addObject("message", "Tragen Sie hier die Künstler des Events ein.");
		mv.addObject("locations", locationDao.findAll());
		mv.addObject("artists", artistDao.findAll());
		mv.addObject("tab", "events");
		mv.setViewName("edit-event");
    	return mv;
    }

    /**
     * <p>Saves a Event.</p>
     * 
     * <p>Expected HTTP POST and request '/saveevent.html'.</p>
     * @return 
     */
    @RequestMapping(value="/admin/saveevent.html", method=RequestMethod.POST)
    public ModelAndView save(@RequestParam(required=true) Integer id, Integer[] slot, String name, String description, String begin, String end, String price, String ticketlink) {
    	if(ticketlink.length() > 8 && !(ticketlink.toLowerCase().startsWith("http://")) && !(ticketlink.toLowerCase().startsWith("https://"))) ticketlink = "http://"+ticketlink;
    	try {
    		price = price.replace(",", ".");
    		SimpleDateFormat sdfC = new SimpleDateFormat("dd.MM.yyyy HH:mm");
			eventDao.createEvent(id, slot, name, description, sdfC.parse(begin), sdfC.parse(end), Double.parseDouble(price), ticketlink);
			return new ModelAndView("redirect:/events.html");
		} catch (Exception e) {
			ModelAndView mv = new ModelAndView("error");
			mv.addObject("message", e.getMessage());
			mv.addObject("tab", "events");
			return mv;
		}
    }

	/**
     * <p>Searches for a specific {@link Event} object</p>
     * 
     * <p>Expected HTTP GET and request '/event.html'.</p>
     */
    @RequestMapping(value="/event.html", method=RequestMethod.GET)
    public ModelAndView show(@RequestParam(required=true) Integer id) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("event", eventDao.findById(id));
		mv.addObject("tab", "events");
		mv.setViewName("show-event");
		return mv;
    }
    
    /**
     * <p>Deletes a event.</p>
     * 
     * <p>Expected HTTP POST and request '/deleteevent.html'.</p>
     */
    @RequestMapping(value="/admin/deleteevent.html", method=RequestMethod.GET)
    public String delete(Event e) {
        eventDao.delete(e);
        return "redirect:/events.html";
    }
   
    
    /**
     * <p>Searches for Events, Artists and Location</p>
     * 
     * <p>Expected HTTP POST and request '/search.html'.</p>
     */
    @RequestMapping(value="/search.html", method=RequestMethod.POST)
    public ModelAndView search(String search_field) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("search");
		mv.addObject("keyword", search_field);
		mv.addObject("tab", "home");
		mv.addObject("artists", artistDao.findByName(search_field));
		mv.addObject("locations", locationDao.findByName(search_field));
		mv.addObject("events", eventDao.findByName(search_field));
		return mv;
    }

}
